using AhpilyServer;
using AhpilyServers;
using GameServer.Cache;
using GameServer.Model;
using Protocol.Code;
using Protocol.Dto;

namespace GameServer.Logic
{
    /// <summary>
    /// 用户逻辑处理层
    /// </summary>
    public class UserHandler : IHeadler
    {
        private UserCache userCache = Caches.User;
        private AccountCache accountCache = Caches.Account;

        public void OnDisconnect(ClientPeer client)
        {
            if(userCache.IsOnline(client))
            {
                userCache.OffLine(client);
            }
        }

        public void OnReceive(ClientPeer client, int SubCode, object value)
        {
            switch (SubCode)
            {
                case UserCode.CREATE_CREQ:
                    create(client, value.ToString());
                    break;
                case UserCode.GET_INFO_CREQ:
                    getInfo(client);
                    break;
                case UserCode.ONLINE_CREQ:
                    online(client);
                    break;
                case UserCode.OFFLINE_CREQ:
                    offline(client);
                    break;
                default:
                    break;
            }
        }
        private void offline(ClientPeer client)
        {
            if(userCache.IsOnline(client))
            {
                userCache.OffLine(client);
                accountCache.Offline(client);
            }
        }
        /// <summary>
        /// 创建角色
        /// </summary>
        private void create(ClientPeer client, string name)
        {
            SingleExcute.Instance.Excute(delegate ()
            {
                // 是否非法登录
                if (!accountCache.IsOnline(client))
                {
                    client.Send(OpCode.USER, UserCode.CREATE_SRES, -1);
                    return;
                }
                // 获取账号id
                int accountId = accountCache.GetId(client);

                // 已经有角色  返回
                if (userCache.IsExit(accountId))
                {
                    client.Send(OpCode.USER, UserCode.CREATE_SRES, -2);
                    return;
                }

                userCache.Create(name, accountId);
                // 创建成功
                client.Send(OpCode.USER, UserCode.CREATE_SRES, 0);
            });
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        private void getInfo(ClientPeer client)
        {
            SingleExcute.Instance.Excute(delegate ()
            {
                // 是否非法登录
                if (!accountCache.IsOnline(client))
                {
                    //client.Send(OpCode.USER, UserCode.GET_INFO_SRES, null);
                    return;
                }
                int accountId = accountCache.GetId(client);
                // 是否次存在角色信息
                if (userCache.IsExit(accountId) == false)
                {
                    client.Send(OpCode.USER, UserCode.GET_INFO_SRES, null);
                    return;
                }
                // 有角色了,上线角色
                if(userCache.IsOnline(client) == false)
                {
                    online(client);
                }

                // 获取角色信息成功, 给客户端发送自己的角色信息
                UserModel model = userCache.GetModelByAccountId(accountId);
                // dto --- Data Transfer Object
                UserDto dto = new UserDto(model.Id, model.Name, model.Been, model.WinCount, model.LoseCount, model.RunCount, model.Lv, model.Exp);
                
                client.Send(OpCode.USER, UserCode.GET_INFO_SRES, dto);
            });
        }

        /// <summary>
        /// 角色上线
        /// </summary>
        /// <param name="client"></param>
        private void online(ClientPeer client)
        {
            SingleExcute.Instance.Excute(delegate ()
            {
                // 是否非法登录
                if (!accountCache.IsOnline(client))
                {
                    client.Send(OpCode.USER, UserCode.ONLINE_SRES, -1);
                    return;
                }
                int accountId = accountCache.GetId(client);
                if (userCache.IsExit(accountId) == false)
                {
                    // 没有角色,不能上线
                    client.Send(OpCode.USER, UserCode.ONLINE_SRES, -2);
                    return;
                }
                // 上线成功
                int userId = userCache.GetId(accountId);
                userCache.Online(client, userId);
                client.Send(OpCode.USER, UserCode.ONLINE_SRES, 0);
            });
        }
    }
}
